const express = require('express');
const { setRoutes } = require('./routes/index');
const { logger } = require('./middlewares/index');


const app = express();
const PORT = process.env.PORT || 3020;

// Middleware
app.use(logger);
app.use(express.json()); // 用于解析application/json
app.use(express.urlencoded({ extended: true }));

// 跨域中间件，允许所有来源跨域请求
app.use((req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.use(express.static('public'));

// Routes
setRoutes(app);

app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});